<template>
  <div>
    <!--        搜索框-->
    <div class="pd-5">
      <el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-search" v-model="query.username"
                class="ml-5 mr-5"></el-input>
      <el-input style="width: 200px" placeholder="请输入邮箱" suffix-icon="el-icon-message" v-model="query.email"
                class="ml-5 mr-5"></el-input>
      <el-input style="width: 200px" placeholder="请输入手机号" suffix-icon="el-icon-phone-outline"
                v-model="query.phone"
                class="ml-5 mr-5"></el-input>
      <el-input style="width: 200px" placeholder="请输入地址" suffix-icon="el-icon-position" v-model="query.address"
                class="ml-5 mr-5"></el-input>
      <el-button type="primary" @click="loadBtn">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
      <div style="float: right">
        <el-button type="primary" @click="saveBtn">新增<i class="el-icon-circle-plus-outline"></i></el-button>
        <el-popconfirm
            class="mr-5 ml-5"
            confirm-button-text='确定'
            cancel-button-text='点错了'
            icon="el-icon-info"
            icon-color="red"
            title="确定删除吗？"
            @confirm="delNumBtn()">
          <el-button type="danger" slot="reference">批量删除<i class="el-icon-remove-outline"></i></el-button>
        </el-popconfirm>
        <el-upload action="http://localhost:8090/user/import"
                   style="display: inline-block"
                   accept="xlsx"
                   :show-file-list="false"
                   :on-success="uploadSuccess">
          <el-button type="primary">导入<i class="el-icon-download"></i></el-button>
        </el-upload>
        <el-button type="primary" @click="exportBtn" class="ml-5">导出<i class="el-icon-upload2"></i></el-button>
      </div>
    </div>

    <!--        列表-->
    <el-table :data="tableData" stripe border :header-cell-class-name="headerBg"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="编号" width="100"></el-table-column>
      <el-table-column prop="username" label="用户名" width="140"></el-table-column>
      <el-table-column prop="nickname" label="昵称" width="140"></el-table-column>
      <el-table-column prop="role" label="角色" width="140"></el-table-column>
      <el-table-column prop="email" label="邮箱" width="140"></el-table-column>
      <el-table-column prop="phone" label="手机号" width="140"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
      <el-table-column label="操作"
                       align="center"
                       fixed="right"
                       width="200px">
        <template slot-scope="scope">
          <el-popconfirm
              class="mr-5 ml-5"
              confirm-button-text='确定'
              cancel-button-text='点错了'
              icon="el-icon-info"
              icon-color="red"
              title="确定删除吗？"
              @confirm="delectBtn(scope.row.id)">
            <el-button type="danger" slot="reference">删除<i class="el-icon-remove-outline"></i></el-button>
          </el-popconfirm>

          <el-button type="success" @click="editBtn(scope.row)">编辑<i class="el-icon-edit"></i></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--        分页-->
    <div class="pd-5">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total=total>
      </el-pagination>
    </div>
    <!--添加用户-->
    <el-dialog title="添加用户" :visible.sync="dialogFormVisible" width="500px">
      <el-form label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="user.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" v-if="!editBtnData">
          <el-input v-model="user.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="user.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="user.role" clearable placeholder="请选择角色" style="width: 100%">
            <el-option v-for="item in roles" :key="item.name" :label="item.description" :value="item.flag"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="user.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="user.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="user.address" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 模态窗口样式
      user: {
        name: '',
        password: '',
        nickname: '',
        address: '',
        phone: '',
        email: '',
        role: '',
      },
      // 分页数据
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 5,

      // 条件查询数据
      query: {
        username: '',
        email: '',
        address: '',
        phone: ''
      },

      // 批量删除数据
      multipleSelection: [],

      //编辑数据
      editBtnData: false,

      //角色信息
      roles: [],

      headerBg: 'headerBg',
      dialogFormVisible: false,
    }
  },
  methods: {
    // 导出文件
    exportBtn() {
      window.open("http://localhost:9090/user/download", '_top')
    },
    //导入文件-成功回调
    uploadSuccess() {
      this.$message.success("上传成功")
      this.load()
    },

    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val
      this.load()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val
      this.load()
    },
    load() {
      this.request.get("/user/page", {
        params: {
          num: this.pageNum,
          pagesize: this.pageSize,

          username: this.query.username,
          email: this.query.email,
          address: this.query.address,
          phone: this.query.phone
        }
      }).then(res => {
            // console.log(res.data)
            if (res.code == 200) {
              this.tableData = res.data.records
              this.total = res.data.total
            }
          },
          Error => {
            console.log(Error)
          }
      )
      this.request.get("/role").then(res=>{
        if (res.code == 200) {
          this.roles=res.data
        }
      })
    },
    reset() {
      this.query.username = ''
      this.query.email = ''
      this.query.phone = ''
      this.query.address = ''
      this.load()
    },
    //搜索
    loadBtn() {
      //需将页面转为第一页，从头开始查询
      this.pageNum = 1
      this.load()
    },
    //添加
    saveBtn() {
      this.dialogFormVisible = true
      this.user = {}
    },
    save() {
      // console.log(this.user)
      this.request.post('/user', this.user).then(
          res => {
            if (res.code == 200) {
              this.load()
            }
          },
          Error => {
            console.log(Error)
          }
      )
      this.dialogFormVisible = false
    },
    //删除
    delectBtn(id) {
      this.request.delete('/user/' + id).then(
          res => {
            if (res.code == 200) {
              this.$message.success("删除成功")
              this.load()
            }
          },
          Error => {
            this.$message.success("删除失败")
            console.log(Error)
          }
      )
    },
    //删除多个
    delNumBtn() {
      let ids = this.multipleSelection.map(v => v.id)
      if (ids.length == 0) {
        this.$message.error("请选择要删除的用户")
        return;
      }
      this.request.delete('/user/batch', {data: ids}).then(res => {
            if (res) {
              if (res.code == 200) {
                this.$message.success("删除成功")
                this.load()
              }
            } else {
              this.$message.error("删除失败")
              console.log(Error)
            }
          }
      )
    },
    //多选框事件
    handleSelectionChange(val) {
      this.multipleSelection = val;
      // console.log(val)
    },
    //编辑
    editBtn(row) {
      this.editBtnData = true
      this.user = row
      this.dialogFormVisible = true
    },
  },
  created() {
    this.load()
  },
}
</script>


<style scoped>

</style>